import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app_study/bean/hot_entity.dart';
import 'package:flutter_app_study/utils/ImageUtil.dart';

class HotFragment extends StatefulWidget {
  @override
  _HotFragmentState createState() => _HotFragmentState();
}

class _HotFragmentState extends State<HotFragment> {
  List<HotData> hotList = [];

  @override
  void initState() {
    // TODO: implement initState
    var hotEntity = HotEntity();
    hotEntity.data = hotList;
    for (var i = 1; i < 30; ++i) {
      var hotData = HotData();
      hotData.title = "我是标题$i";
      hotData.picUrl = ImageUtil.getImageAsset("pic$i.jpg");
      hotList.add(hotData);
    }
    var str = json.encode(hotEntity);
    print(str);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          if (index == 0) {
            return TitleWidget((index) {
              setState(() {
                hotList.shuffle();
              });
              print("点击了$index");
            });
          } else {
            return ItemWidget(hotList[index - 1]);
          }
        },
        itemCount: hotList.length + 1,
        padding: EdgeInsets.only(bottom: 45),
      ),
    );
  }
}

class TitleWidget extends StatefulWidget {
  Function(int index) onclick;

  TitleWidget(this.onclick);

  @override
  _TitleWidgetState createState() => _TitleWidgetState();
}

class _TitleWidgetState extends State<TitleWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: 5, bottom: 5),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          InkWell(
            child: Column(
              children: <Widget>[
                Icon(Icons.add),
                Text("排行榜"),
              ],
            ),
            onTap: () {
              widget.onclick(7777);
            },
          ),
          Column(
            children: <Widget>[
              Icon(Icons.add),
              Text("每周必看"),
            ],
          ),
          Column(
            children: <Widget>[
              Icon(Icons.add),
              Text("抗击肺炎"),
            ],
          ),
          Column(
            children: <Widget>[
              Icon(Icons.add),
              Text("入站必刷"),
            ],
          ),
        ],
      ),
    );
  }
}

class ItemWidget extends StatefulWidget {
  HotData hotList;

  ItemWidget(this.hotList);

  @override
  _ItemWidgetState createState() => _ItemWidgetState();
}

class _ItemWidgetState extends State<ItemWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 10),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Stack(
            alignment: Alignment.bottomRight,
            children: <Widget>[
              Image.asset(
                widget.hotList.picUrl,
                width: 150,
                height: 80,
                fit: BoxFit.fill,
              ),
              Text("03:50"),
            ],
          ),
          Container(
            margin: EdgeInsets.only(left: 10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(widget.hotList.title),
                Container(
                  margin: EdgeInsets.only(top: 2),
                  color: Colors.deepOrangeAccent,
                  child: Text(
                    "7万点赞",
                    style: TextStyle(color: Colors.white, fontSize: 10),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 2),
                  child: Text(
                    "blibili",
                    style: TextStyle(fontSize: 10),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 2),
                  child: Text(
                    "1.1万观看 10小时前",
                    style: TextStyle(fontSize: 10),
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}
